//    Copyright (C) Kherty.  All rights reserved.
using OpenLS.Core.Serialization;
using OpenLS.Spreadsheet.Expressions;

namespace OpenLS.Spreadsheet
{
    public class WorksheetView : SheetView
    {
        public WorksheetView(Worksheet ws): base (ws)
        {
            _worksheet = ws;
        }
        private readonly Worksheet _worksheet;

        public Worksheet Worksheet
        {
            get { return _worksheet; }
        }




        #region IOfficeXmlSerializable Members


        internal void PostReadXml()
        {
            if (_topLeftCache != null)
            {
                //\\TopLeft = CellAddress.Parse(_topLeftCache);
            }
            InternalSelection s = TopLeftSelection;
            if (Pane != null)
            {
                switch (Pane.ActivePane)
                {
                    case PaneType.TopRight:
                        s = TopRightSelection; break;
                    case PaneType.BottomLeft: s = BottomLeftSelection; break;
                    case PaneType.BottomRight: s = BottomRightSelection; break;

                }
            }
            if (s != null)
            {

                if (s.ActiveCell != null)
                {
                    CellAddress a;
                    Worksheet.Workbook.NamingConvention.TryParse(s.ActiveCell, out a);
                    ActiveCellPosition = a;  //\\CoordinateConverter.ToCoordinate(internalSelection.ActiveCell);
                }
                if (s.Selection != null)
                    Selection = Worksheet.GetRange(s.Selection);
            }
        }
        internal override void WriteTopLeft(WriteContext c)
        {
            var pane = this.Worksheet.BottomRightPane;
            if (pane == null)
                pane = this.Worksheet.TopRightPane;
            if (pane == null)
                pane = this.Worksheet.BottomLeftPane;
            if (pane == null)
            {
                pane = this.Worksheet.TopLeftPane;
            }
            if (pane != null && Pane != null)
                this.Pane.TopLeftCell = Worksheet.Workbook.NamingConvention.GetText( new CellReferenceToken(pane.Top, false, pane.Left, false));
            if (Worksheet.TopLeftPane != null)
                c.SetString("topLeftCell",
                    Worksheet.Workbook.NamingConvention.GetText( new CellReferenceToken(Worksheet.TopLeftPane.Top, false, Worksheet.TopLeftPane.Left, false)));
            
            //\\if (TopLeft != null)
            {
             //\\   c.SetString("topLeftCell", this.Worksheet.Workbook.NamingConvention.Address(TopLeft.Row, TopLeft.Column, false, false));
            }
        }


	

        #endregion

        internal void PreWrite(Worksheet worksheet)
        {
            if (worksheet.Selection != null)
                _selection = worksheet.Selection;
            else
                _selection = null;
            if (worksheet.ActiveCell != null)
                ActiveCellPosition = worksheet.ActiveCell.Address;
            else
                ActiveCellPosition = null;
        }
    }
}
